﻿Public Class frmModulos
    Dim cambio As Boolean
    Dim webQuest As Boolean
    Dim serie2R As Boolean
    Dim serie3R As Boolean
    Dim paralelo2R As Boolean
    Dim paralelo3R As Boolean
    Dim mixtos3R As Boolean
    Dim mixtos4R As Boolean

    Private Sub frmModulos_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        compararEstadoModulos()
        If cambio Then
            If MessageBox.Show("¿Desea cancelar los cambios?", "Confirmación", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = MsgBoxResult.No Then
                e.Cancel = True
            End If
        End If
    End Sub


    Private Sub frmModulos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        obtenerEstadoModulos()
        asignarEstadoModulos()
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
    End Sub

    Private Sub btnAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAceptar.Click
        compararEstadoModulos()
        If cambio Then
            If MessageBox.Show("¿Desea guardar los cambios?", "Confirmación", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = MsgBoxResult.Yes Then
                modificarEstadoModulos()
            End If
            Me.Close()
        Else
            Me.Close()
        End If
        
    End Sub

    Private Sub chkSerie_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSerie.CheckedChanged
        If chkSerie.Checked Then
            chkSerie2R.Enabled = True
            chkSerie3R.Enabled = True
            chkSerie2R.Checked = True
            chkSerie3R.Checked = True
        Else
            chkSerie2R.Checked = False
            chkSerie3R.Checked = False
            chkSerie2R.Enabled = False
            chkSerie3R.Enabled = False
        End If
    End Sub

    Private Sub chkParalelo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkParalelo.CheckedChanged
        If chkParalelo.Checked Then
            chkParalelo2R.Enabled = True
            chkParalelo3R.Enabled = True
            chkParalelo2R.Checked = True
            chkParalelo3R.Checked = True
        Else
            chkParalelo2R.Enabled = False
            chkParalelo3R.Enabled = False
            chkParalelo2R.Checked = False
            chkParalelo3R.Checked = False
        End If
    End Sub

    Private Sub chkMixtos_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkMixtos.CheckedChanged
        If chkMixtos.Checked Then
            chkMixtos4R.Enabled = True
            chkMixtos3R.Enabled = True
            chkMixtos4R.Checked = True
            chkMixtos3R.Checked = True
        Else
            chkMixtos4R.Enabled = False
            chkMixtos3R.Enabled = False
            chkMixtos4R.Checked = False
            chkMixtos3R.Checked = False
        End If
    End Sub
    Private Sub obtenerEstadoModulos()
        Dim consulta As String
        Dim tabla As DataTable
        consulta = "SELECT * FROM Modulo"
        tabla = ejecutarConsulta(consulta)
        'WEBQUEST
        If tabla(0)("habilitado") = True Then
            webQuest = True
        Else
            webQuest = False
        End If

        'SERIE
        If tabla(1)("habilitado") = True Then
            serie2R = True
        Else
            serie2R = False
        End If
        If tabla(2)("habilitado") = True Then
            serie3R = True
        Else
            serie3R = False
        End If

        'PARALELO
        If tabla(3)("habilitado") = True Then
            paralelo2R = True
        Else
            paralelo2R = False
        End If
        If tabla(4)("habilitado") = True Then
            paralelo3R = True
        Else
            paralelo3R = False
        End If

        'MIXTOS
        If tabla(5)("habilitado") = True Then
            mixtos3R = True
        Else
            mixtos3R = False
        End If
        If tabla(6)("habilitado") = True Then
            mixtos4R = True
        Else
            mixtos4R = False
        End If

    End Sub
    'Private Sub consultarModulos()
    '    Dim consulta As String
    '    Dim tabla As DataTable
    '    consulta = "SELECT * FROM Modulo"
    '    tabla = ejecutarConsulta(consulta)
    '    'WEBQUEST
    '    If tabla(0)("habilitado") = True Then
    '        chkWebQuest.Checked = True
    '    Else
    '        chkWebQuest.Checked = False
    '    End If

    '    'SERIE
    '    If tabla(1)("habilitado") = True Then
    '        chkSerie.Checked = True
    '        chkSerie2R.Checked = True
    '    Else
    '        chkSerie2R.Checked = False
    '    End If
    '    If tabla(2)("habilitado") = True Then
    '        chkSerie.Checked = True
    '        chkSerie3R.Checked = True
    '    Else
    '        chkSerie3R.Checked = False
    '    End If

    '    If chkSerie2R.Checked = False And chkSerie3R.Checked = False Then
    '        chkSerie.Checked = False
    '        chkSerie2R.Enabled = False
    '        chkSerie3R.Enabled = False
    '    End If

    '    'PARALELO
    '    If tabla(3)("habilitado") = True Then
    '        chkParalelo.Checked = True
    '        chkParalelo2R.Checked = True
    '    Else
    '        chkParalelo2R.Checked = False
    '    End If
    '    If tabla(4)("habilitado") = True Then
    '        chkParalelo.Checked = True
    '        chkParalelo3R.Checked = True
    '    Else
    '        chkParalelo3R.Checked = False
    '    End If

    '    If chkParalelo2R.Checked = False And chkParalelo3R.Checked = False Then
    '        chkParalelo.Checked = False
    '        chkParalelo2R.Enabled = False
    '        chkParalelo3R.Enabled = False
    '    End If

    '    'MIXTOS
    '    If tabla(5)("habilitado") = True Then
    '        chkMixtos.Checked = True
    '        chkMixtos3R.Checked = True
    '    Else
    '        chkMixtos3R.Checked = False
    '    End If
    '    If tabla(6)("habilitado") = True Then
    '        chkMixtos.Checked = True
    '        chkMixtos4R.Checked = True
    '    Else
    '        chkMixtos4R.Checked = False
    '    End If

    '    If chkMixtos3R.Checked = False And chkMixtos4R.Checked = False Then
    '        chkMixtos.Checked = False
    '        chkMixtos3R.Enabled = False
    '        chkMixtos4R.Enabled = False
    '    End If

    'End Sub
    Private Sub asignarEstadoModulos()
        'WEBQUEST
        chkWebQuest.Checked = webQuest

        'SERIE
        chkSerie2R.Checked = serie2R
        chkSerie3R.Checked = serie3R

        If serie2R = False And serie3R = False Then
            chkSerie.Checked = False
            chkSerie2R.Enabled = False
            chkSerie3R.Enabled = False
        Else
            chkSerie.Checked = True
        End If

        'PARALELO
        chkParalelo2R.Checked = paralelo2R
        chkParalelo3R.Checked = paralelo3R

        If paralelo2R = False And paralelo3R = False Then
            chkParalelo.Checked = False
            chkParalelo2R.Enabled = False
            chkParalelo3R.Enabled = False
        Else
            chkParalelo.Checked = True
        End If

        'MIXTOS
        chkMixtos3R.Checked = mixtos3R
        chkMixtos4R.Checked = mixtos4R

        If mixtos3R = False And mixtos4R = False Then
            chkMixtos.Checked = False
            chkMixtos3R.Enabled = False
            chkMixtos4R.Enabled = False
        Else
            chkMixtos.Checked = True
        End If

    End Sub

    Private Sub compararEstadoModulos()
        cambio = False
        If webQuest <> chkWebQuest.Checked Then
            cambio = True
        End If
        If serie2R <> chkSerie2R.Checked Then
            cambio = True
        End If
        If serie3R <> chkSerie3R.Checked Then
            cambio = True
        End If
        If paralelo2R <> chkParalelo2R.Checked Then
            cambio = True
        End If
        If paralelo3R <> chkParalelo3R.Checked Then
            cambio = True
        End If
        If mixtos3R <> chkMixtos3R.Checked Then
            cambio = True
        End If
        If mixtos4R <> chkMixtos4R.Checked Then
            cambio = True
        End If
    End Sub

    Private Sub modificarEstadoModulos()
        'WEBQUEST
        webQuest = chkWebQuest.Checked

        'SERIE
        serie2R = chkSerie2R.Checked
        serie3R = chkSerie3R.Checked

        'PARALELO
        paralelo2R = chkParalelo2R.Checked
        paralelo3R = chkParalelo3R.Checked

        'MIXTOS
        mixtos3R = chkMixtos3R.Checked
        mixtos4R = chkMixtos4R.Checked

        Dim consulta As String
        consulta = "UPDATE Modulo SET habilitado=" + webQuest.ToString + " WHERE id=1"
        ejecutarTransaccion(consulta)
        consulta = "UPDATE Modulo SET habilitado=" + serie2R.ToString + " WHERE id=2"
        ejecutarTransaccion(consulta)
        consulta = "UPDATE Modulo SET habilitado=" + serie3R.ToString + " WHERE id=3"
        ejecutarTransaccion(consulta)
        consulta = "UPDATE Modulo SET habilitado=" + paralelo2R.ToString + " WHERE id=4"
        ejecutarTransaccion(consulta)
        consulta = "UPDATE Modulo SET habilitado=" + paralelo3R.ToString + " WHERE id=5"
        ejecutarTransaccion(consulta)
        consulta = "UPDATE Modulo SET habilitado=" + mixtos3R.ToString + " WHERE id=6"
        ejecutarTransaccion(consulta)
        consulta = "UPDATE Modulo SET habilitado=" + mixtos4R.ToString + " WHERE id=7"
        ejecutarTransaccion(consulta)

    End Sub

    Private Sub reiniciar()
        chkSerie.Checked = True
        chkParalelo.Checked = True
        chkMixtos.Checked = True
        chkWebQuest.Checked = True
    End Sub
End Class